import json

from app.model import mysql
from app.service.user import add_user_custom_tags


async def get_media(media_id) -> mysql.Media:
    """
    获取媒体
    :param media_id:
    :return:
    """
    media = await mysql.Media.get_or_none(id=media_id)
    if media:
        if media.object_type == mysql.Media.Type.photo:
            media.data = await mysql.Photo.get_or_none(id=media.object_id)
        elif media.object_type == mysql.Media.Type.case:
            media.data = await mysql.Case.get_or_none(id=media.object_id)
        elif media.object_type == mysql.Media.Type.video:
            media.data = await mysql.Video.get_or_none(id=media.object_id)
        elif media.object_type == mysql.Media.Type.panorama:
            media.data = await mysql.Panorama.get_or_none(id=media.object_id)
        else:
            media.data = None
    return media


async def get_base_media(media_id):
    """
    获取媒体基本信息
    :param media_id:
    :return:
    """
    return await mysql.Media.get_or_none(id=media_id)


async def get_my_media(user_id, media_id):
    """
    获取我的媒体
    :param user_id:
    :param media_id:
    :return:
    """
    return await mysql.MyMedia.get_or_none(user_id=user_id, media_id=media_id)


async def get_media_custom_tags(user_id, media_id):
    """
    获取媒体自定义标签
    :param user_id:
    :param media_id:
    :return:
    """
    info = await mysql.MediaCustomTag.get_or_none(user_id=user_id, media_id=media_id)
    custom_tags = []
    if info and info.tags:
        try:
            custom_tags = json.loads(info.tags)
        except Exception as e:
            custom_tags = []
    return custom_tags


async def update_media_custom_tags(user_id, media_id, custom_tags: list):
    """
    更新媒体自定义标签
    :param user_id:
    :param media_id:
    :param custom_tags:
    :return:
    """
    info, is_new = await mysql.MediaCustomTag.get_or_create(user_id=user_id, media_id=media_id)
    info.tags = json.dumps(custom_tags, ensure_ascii=False)
    await info.save()


async def update_media_base_info(user_id, media_id, title, custom_tags):
    """
    修改媒体基本信息
    :param user_id:
    :param media_id:
    :param title:
    :param custom_tags:
    :return:
    """
    media = await mysql.Media.get_or_none(id=media_id)
    if media:
        # 更新标题
        if media.user_id == user_id:
            if media.object_type == mysql.Media.Type.photo:
                await mysql.Photo.filter(id=media.object_id).update(text=title)
            elif media.object_type == mysql.Media.Type.case:
                await mysql.Case.filter(id=media.object_id).update(title=title)
            elif media.object_type == mysql.Media.Type.video:
                await mysql.Video.filter(id=media.object_id).update(text=title)
            elif media.object_type == mysql.Media.Type.panorama:
                await mysql.Panorama.filter(id=media.object_id).update(text=title)
        # 更新自定义标签
        await update_media_custom_tags(user_id, media_id, custom_tags)
        # 更新用户自定义标签信息
        await add_user_custom_tags(user_id, custom_tags)


async def is_collect_media(user_id: int, media_id: int):
    """
    获取是否收藏媒体
    :param user_id:
    :param media_id:
    :return:
    """
    count = await mysql.Collect.filter(user_id=user_id, media_id=media_id).count()
    return count > 0


async def is_like_media(user_id: int, media_id: int):
    """
    判断是否点赞媒体
    :param user_id:
    :param media_id:
    :return:
    """
    count = await mysql.Like.filter(user_id=user_id, media_id=media_id).count()
    return count > 0
